<template>
  <div class="topbar">
    <div class="topbarWrap wrap">
      <div class="left">欢迎来到叩丁狼积分商城</div>
      <ul class="right">
        <li>
          <img :src="userInfo.headImg" alt="" />
          用户名：{{ userInfo.nickName }}
        </li>
        <li>我的积分：{{ userInfo.coin }}🍗</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="cart-btn" v-if="isLogined">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>{{ cartTotal }}</strong>
        </li>
        <li v-else class="loginBtn" @click="changeIsShowLoginModal(true)">
          登录
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations, mapActions } from "vuex";
import { PCLoginApi } from "@/request/api";
export default {
  data() {
    return {
      //购物车总数
    };
  },
  computed: {
    ...mapState({
      isLogined: (state) => state.loginStatus.isLogined,
      userInfo: (state) => state.userInfo.userInfo,
      cartTotal: (state) => state.userInfo.cartTotal,
    }),
  },
  methods: {
    ...mapMutations({
      changeIsShowLoginModal: "showModel/changeIsShowLoginModal",
      changeIsLogined: "loginStatus/changeIsLogined",
    }),
    ...mapActions({
      asyncChangeUserInfo: "userInfo/asyncChangeUserInfo",
    }),
  },
  async created() {
    let code = this.$route.query.code;
    console.log({ code });
    if (code) {
      // 微信登录第二步：临时票据code换取token
      let res = await PCLoginApi({
        //有code去换token
        code,
      });
      console.log("微信扫码res：", res);

      if (res.code == 0) {
        alert("微信扫码登陆成功");
        // 切换登录状态，显示购物车
        this.changeIsLogined(true);
        localStorage.setItem("x-auth-token", res["x-auth-token"]);
        this.$router.push(this.$route.path);

        // 登录成功后，更新用户登录状态
        this.asyncChangeUserInfo();
      } else if (res.code == 400) {
        alert("二维码失效，请重新扫码");
        // 弹出登录框
        this.changeIsShowLoginModal(true);
      } else if (res.code == 407) {
        //用户的微信还没有在这个网站上绑定过手机
        //使用手机号登录并携带uuid，由后端自动绑定
        alert("请再用手机号登陆一次");
        this.changeIsShowLoginModal(true);
        localStorage.setItem("uuid", res.uuid);
      }
    } else {
      let token = localStorage.getItem("x-auth-token");
      /* if (token) {
        this.changeIsLogined(true);
      } else {
        this.changeIsLogined(false);
      } */
      // 通过转换成布尔值，判断状态
      // this.changeIsLogined(Boolean(token));
      this.changeIsLogined(!!token);
      /* 
        利用取反的隐性转换为布尔值，优化代码
        console.log(!!token);
        console.log(!token); 
      */
      if (token) {
        //获取用户信息
        this.asyncChangeUserInfo();
      } else {
      }
    }
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less">
.topbar {
  min-width: 1200px;
  height: 40px;
  line-height: 40px;
  background: #242b39;
  text-align: center;
  font-size: 14px;
  font-family: SourceHanSansSC;
  font-weight: 300;
  color: #fffefe;
  .topbarWrap {
    display: flex;
    justify-content: space-between;
    li {
      float: left;
      margin-left: 20px;
      cursor: pointer;
      img {
        width: 22px;
        vertical-align: middle;
        padding-bottom: 2px;
        border-radius: 50%;
      }
      &.loginBtn {
        width: 124px;
        background: #0a328e;
        /* display: flex;
        justify-content: space-between;
        align-items: center;
        flex:1 */
      }
      &.cart-btn {
        width: 124px;
        height: 40px;
        background: #0a328e;
        text-align: center;
        img {
          width: 20px;
          height: 19px;
          vertical-align: middle;
        }
        span {
          margin: 0 8px;
        }
        strong {
          width: 22px;
          height: 22px;
          line-height: 22px;
          display: inline-block;
          border-radius: 50%;
          background-color: #fd604d;
        }
      }
    }
  }
}
</style>
